﻿using K9Nano.Dependency;

namespace K9Nano.Authorization;

internal class PermissionCatalog : IPermissionCatalog, ISingletonDependency
{
    private readonly HashSet<Permission> _permissions = new(new PermissionEqualityComparer());

    public IEnumerable<Permission> Permissions => _permissions;

    public IPermissionCatalog Add(Permission permission)
    {
        if (_permissions.Contains(permission))
        {
            throw new Exception("不能重复添加权限：" + permission.Name);
        }
        _permissions.Add(permission);
        return this;
    }

    public bool TryAdd(Permission permission)
    {
        return _permissions.Add(permission);
    }
}